﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace QuanLyNhanVien.DataAccessLayer
{
    class NgayNghiDAO : DataProvider
    {
        private string URLNgayNghi = URL + "/api/NghiPhep";
        public List<string> GetColName()
        {
            //List<string> cols = new List<string>();
            //string query = "SELECT * FROM NGAYNGHI";
            //DataTable dt = new DataTable();
            //dt = executeQuery(query);
            //for (int i = 0; i < dt.Columns.Count; i++)
            //{
            //    cols.Add(dt.Columns[i].ToString());
            //}
            //return cols;

            List<string> result = new List<string>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "/api/Tables/NGHIPHEP");
            request.Method = "GET";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {

                            string response = responseReader.ReadToEnd();
                            result = JsonConvert.DeserializeObject<List<string>>(response);

                        }
                    }
                }
            }
            catch (Exception e)
            {
            }

            return result;

        }

        public List<NgayNghiDTO> LayDanhSachNgayNghi()
        {
            List<NgayNghiDTO> result = new List<NgayNghiDTO>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNgayNghi);
            request.Method = "GET";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {

                            string response = responseReader.ReadToEnd();
                            result = JsonConvert.DeserializeObject<List<NgayNghiDTO>>(response);

                        }
                    }
                }
            }
            catch (Exception e)
            {
            }

            return result;
           
        }

        //public List<NgayNghiDTO> LayDanhSachNgayNghi(string MANV, int Thang, int Nam)
        //{

        //    //string query = "SELECT * FROM NGAYNGHI WHERE MANV = '" + MANV + "' AND Thang = " + Thang + " AND Nam = " + Nam + " ORDER BY ID ASC";
        //    ////adapter = new OleDbDataAdapter(query, connection);
        //    ////DataTable dt = new DataTable();
        //    ////adapter.Fill(dt);
        //    //DataTable dt = new DataTable();
        //    //dt = executeQuery(query);
        //    //List<NgayNghiDTO> result = new List<NgayNghiDTO>();

        //    //for (int i = 0; i < dt.Rows.Count; i++)
        //    //{
        //    //    NgayNghiDTO nn = new NgayNghiDTO();
        //    //    nn.ID = (int)dt.Rows[i]["ID"];
        //    //    nn.MANV = (string)dt.Rows[i]["MANV"];
        //    //    nn.Thang = (int)dt.Rows[i]["Thang"];
        //    //    nn.Nam = (int)dt.Rows[i]["Nam"];
        //    //    nn.SoNgayNghi = (int)dt.Rows[i]["SoNgayNghi"];
        //    //    result.Add(nn);
        //    //}

        //    //return result;
        //}

        public bool ThemNgayNghi(NgayNghiDTO nn)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNgayNghi);

            request.Method = "POST";
            request.ContentType = "application/json";



            JObject jObject = JObject.FromObject(nn);
            string data = jObject.ToString(Newtonsoft.Json.Formatting.None);


            request.ContentLength = data.Length;




            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write(data);
            }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;
        }

        public bool XoaNgayNghi(string ID)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNgayNghi + "/" + ID);

            request.Method = "DELETE";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();

                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;
        }

        public bool SuaNgayNghi(NgayNghiDTO nn)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNgayNghi + "/" + nn.ID);

            request.Method = "PUT";
            request.ContentType = "application/json";

            JObject jObject = JObject.FromObject(nn);
            string data = jObject.ToString(Newtonsoft.Json.Formatting.None);


            request.ContentLength = data.Length;




            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write(data);
            }


            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;
        }
    }
}
